<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\Db;
use think\Controller;
use think\Request;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class UserWallet  extends Backend
{

    /**
     * UserWarehouse模型对象
     */
    protected $model = null;
    protected $warehouse_items;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('user_wallet');
        //$this->warehouse_items = config('user_warehouse');

    }
    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if($sort=='user_id'){
                $sort='uw.user_id';
            }
            $where = array();
            $filter=input();
            $filters = json_decode($filter['filter'], true);
            if(!empty($filters['user_id'])){
                $where['uw.user_id']=$filters['user_id'];
            }
            if(!empty($filters['user_name'])){
                $where['u.user_name']=$filters['user_name'];
            }
            if(!empty($filters['full_name'])){
                $where['u.full_name']=array('like','%'.$filters['full_name'].'%');
            }
            if(!empty($filters['last_time'])) {
                $date = explode(',', $filters['last_time']);
                $where['u.last_time'] = array('between', $date);
            }
            $where['uw.is_paid']=1;
            $total = $this->model
                ->alias('uw')
                ->join('users u', 'u.user_id = uw.user_id')
                ->where($where)
                ->order($sort, $order)
                ->group('uw.user_id')
                ->count();
            //$total = count($this->model->left_join());
            $list = $this->model
                ->alias('uw')
                ->join('users u', 'u.user_id = uw.user_id')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->group('uw.user_id')
                ->select();
            $lists=$this->model->left_join($list);
            //$list = $this->model->left_join($where,$sort,$order,$offset,$limit);
            $result = array("total" => $total, "rows" => $lists);

            return json($result);
        }
        $btns = ['refresh'];
        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
}